const fs = window.require("fs");

export default function storeData(state, { that }) {
  let data = JSON.stringify(state.page.tree);

  //对图片进行替换
  for (const key in that.imgList) {
    if (Object.hasOwnProperty.call(that.imgList, key)) {
      data = data.replace(that.imgList[key].blob, that.imgList[key].src);
    }
  }

  //写入文件
  try {
    fs.writeFileSync(that.__filePath + "/pageData.json", data);
    that.$message({
      message: "保存成功",
      type: "success",
    });
  } catch (error) {
    that.$message.error("保存失败，请重试!");
    console.error(error);
  }
}
